1use serde::Serialize;
16
17#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
33#[serde(rename_all = "snake_case")]
34#[non_exhaustive]
35pub enum CheckStatus {
36 Passed,
37 Failed,
38 Skipped,
39 Unknown,
40 Info,
42}
43
44impl CheckStatus {
45 pub const fn as_str(self) -> &'static str {
46 match self {
47 Self::Passed => "passed",
48 Self::Failed => "failed",
49 Self::Skipped => "skipped",
50 Self::Unknown => "unknown",
51 Self::Info => "info",
52 }
53 }
54
55 pub const fn blocks(self) -> bool {
57 matches!(self, Self::Failed | Self::Skipped | Self::Unknown)
58 }
59}
60
61#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
67#[serde(rename_all = "snake_case")]
68#[non_exhaustive]
69pub enum Verdict {
70 Valid,
72 Indeterminate,
74 Invalid,
76}
77
78impl Verdict {
79 pub const fn as_str(self) -> &'static str {
80 match self {
81 Self::Valid => "valid",
82 Self::Indeterminate => "indeterminate",
83 Self::Invalid => "invalid",
84 }
85 }
86
87 pub fn worst(self, other: Self) -> Self {
90 if other > self { other } else { self }
91 }
92}
93
94macro_rules! check_codes {
95 ($( $variant:ident => $text:literal ),* $(,)?) => {
96 #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
104 #[serde(into = "&'static str")]
105 #[non_exhaustive]
106 pub enum CheckCode { $( $variant, )* }
107
108 impl CheckCode {
109 pub const fn as_str(self) -> &'static str {
110 match self { $( Self::$variant => $text, )* }
111 }
112
113 pub const ALL: &'static [Self] = &[ $( Self::$variant, )* ];
115 }
116 };
117}
118
119check_codes! {
120 NoSignatures => "no_signatures",
122 SignatureCountWithinLimits => "signature_count_within_limits",
123 SignatureLimitExceeded => "signature_limit_exceeded",
124 SignaturesUnsupported => "signatures_unsupported",
125
126 DocumentsAllCovered => "documents_all_covered",
128 DocumentsUncovered => "documents_uncovered",
129 DocumentsCoverageUndetermined => "documents_coverage_undetermined",
130
131 SigStructure => "sig_structure",
133 SigStructureInvalid => "sig_structure_invalid",
134 SigPlacement => "sig_placement",
135 SigPlacementInvalid => "sig_placement_invalid",
136 C14nMethodAllowed => "c14n_method_allowed",
137 C14nUnsupported => "c14n_unsupported",
138 SignatureAlgorithmAllowed => "signature_algorithm_allowed",
139 DigestAlgorithmAllowed => "digest_algorithm_allowed",
140 AlgorithmRejected => "algorithm_rejected",
141 AlgorithmLegacyAllowed => "algorithm_legacy_allowed",
142 TransformsAllowed => "transforms_allowed",
143 TransformNotAllowed => "transform_not_allowed",
144 ReferencesSameDocument => "references_same_document",
145 ReferenceExternal => "reference_external",
146 ReferencesResolve => "references_resolve",
147 ReferenceUnresolved => "reference_unresolved",
148 ReferenceScopeComplete => "reference_scope_complete",
149 ReferenceScopeIncomplete => "reference_scope_incomplete",
150 ReferenceScopeUnknown => "reference_scope_unknown",
151
152 CountersignatureBindingOk => "countersignature_binding_ok",
156 CountersignatureBindingMissing => "countersignature_binding_missing",
157 CountersignatureBindingMismatch => "countersignature_binding_mismatch",
158 NestedSignaturesUnsupported => "nested_signatures_unsupported",
159
160 ReferenceDigestOk => "reference_digest_ok",
162 ReferenceDigestMismatch => "reference_digest_mismatch",
163 SignedInfoCanonicalization => "signedinfo_canonicalization",
164 SignedInfoCanonicalizationFailed => "signedinfo_canonicalization_failed",
165 SignatureValueOk => "signature_value_ok",
166 SignatureValueInvalid => "signature_value_invalid",
167
168 XadesPresent => "xades_present",
170 XadesAbsent => "xades_absent",
171 XadesNotValidated => "xades_not_validated",
172 XadesExtraQualifyingProperties => "xades_extra_qualifying_properties",
173 XadesSigningCertificateBound => "xades_signing_certificate_bound",
174 XadesSigningCertificateMismatch => "xades_signing_certificate_mismatch",
175 XadesSigningCertificateAbsent => "xades_signing_certificate_absent",
176 XadesSignaturePolicyImplied => "xades_signature_policy_implied",
177 XadesSignaturePolicyExplicit => "xades_signature_policy_explicit",
178
179 SigningCertificateAvailable => "signing_certificate_available",
181 SigningCertificateMissing => "signing_certificate_missing",
182 SigningTimePresent => "signing_time_present",
183 CertMalformed => "cert_malformed",
184 CertPathOk => "cert_path_ok",
185 CertPathUnknown => "cert_path_unknown",
186 CertPathUntrusted => "cert_path_untrusted",
187 CertPathSearchExhausted => "cert_path_search_exhausted",
188 CertPathLengthExceeded => "cert_path_length_exceeded",
189 CertExpired => "cert_expired",
190 CertNotYetValid => "cert_not_yet_valid",
191 CertSignatureInvalid => "cert_signature_invalid",
192 CertAlgorithmRejected => "cert_algorithm_rejected",
193 CertKeyUsageInvalid => "cert_key_usage_invalid",
194 CertKeyUsageAdvisory => "cert_key_usage_advisory",
195 CertBasicConstraintsInvalid => "cert_basic_constraints_invalid",
196 CertNameConstraintViolation => "cert_name_constraint_violation",
197 CertUnsupportedCriticalExtension => "cert_unsupported_critical_extension",
198
199 RevocationPolicy => "revocation_policy",
201 RevocationNotChecked => "revocation_not_checked",
202 RevocationOk => "revocation_ok",
203 CertRevoked => "cert_revoked",
204 CertRevokedAfterValidationTime => "cert_revoked_after_validation_time",
205 RevocationStatusUnknown => "revocation_status_unknown",
206 RevocationStatusUnknownByResponder => "revocation_status_unknown_by_responder",
207 RevocationDataStale => "revocation_data_stale",
208 RevocationDataInvalid => "revocation_data_invalid",
209 OcspResponderTrusted => "ocsp_responder_trusted",
210 RevocationSourcesDisagree => "revocation_sources_disagree",
211 OnlineFetchFailed => "online_fetch_failed",
212
213 TrustListLoaded => "trust_list_loaded",
215 TrustListUnverified => "trust_list_unverified",
216 TrustListSignatureOk => "trust_list_signature_ok",
217 TrustListSignatureInvalid => "trust_list_signature_invalid",
218 TrustListSignerMismatch => "trust_list_signer_mismatch",
219 TrustListSignerUnqualified => "trust_list_signer_unqualified",
220 TrustListServiceNotGranted => "trust_list_service_not_granted",
221 CertificateQualified => "certificate_qualified",
222 CertificateNotQualified => "certificate_not_qualified",
223 CertificateQualifiedUnknown => "certificate_qualified_unknown",
224
225 SignatureTimestampPresent => "signature_timestamp_present",
227 SignatureTimestampAbsent => "signature_timestamp_absent",
228 TimestampNotChecked => "timestamp_not_checked",
229 TimestampTokenParsed => "timestamp_token_parsed",
230 TimestampImprintOk => "timestamp_imprint_ok",
231 TimestampImprintMismatch => "timestamp_imprint_mismatch",
232 TimestampSignatureOk => "timestamp_signature_ok",
233 TimestampSignatureInvalid => "timestamp_signature_invalid",
234 TimestampTsaCertificateOk => "timestamp_tsa_certificate_ok",
235 TimestampTsaCertificateInvalid => "timestamp_tsa_certificate_invalid",
236 TimestampTsaPathOk => "timestamp_tsa_path_ok",
237 TimestampTsaPathUntrusted => "timestamp_tsa_path_untrusted",
238 TimestampTsaPathUnknown => "timestamp_tsa_path_unknown",
239 TimestampBeforeSigningTime => "timestamp_before_signing_time",
240 TimestampVerified => "timestamp_verified",
241 ArchiveTimestampPresent => "archive_timestamp_present",
242
243 DossierTimestampVerified => "dossier_timestamp_verified",
245 DossierTimestampInvalid => "dossier_timestamp_invalid",
246 DossierTimestampNotChecked => "dossier_timestamp_not_checked",
247 DocumentTimestampVerified => "document_timestamp_verified",
248 DocumentTimestampInvalid => "document_timestamp_invalid",
249 DocumentTimestampNotChecked => "document_timestamp_not_checked",
250}
251
252impl From<CheckCode> for &'static str {
253 fn from(code: CheckCode) -> Self {
254 code.as_str()
255 }
256}
257
258#[derive(Clone, Debug, Serialize)]
260pub struct Check {
261 pub code: CheckCode,
262 pub status: CheckStatus,
263 pub message: String,
266}
267
268impl Check {
269 pub fn new(code: CheckCode, status: CheckStatus, message: impl Into<String>) -> Self {
270 Self {
271 code,
272 status,
273 message: message.into(),
274 }
275 }
276
277 pub fn passed(code: CheckCode, message: impl Into<String>) -> Self {
278 Self::new(code, CheckStatus::Passed, message)
279 }
280
281 pub fn failed(code: CheckCode, message: impl Into<String>) -> Self {
282 Self::new(code, CheckStatus::Failed, message)
283 }
284
285 pub fn skipped(code: CheckCode, message: impl Into<String>) -> Self {
286 Self::new(code, CheckStatus::Skipped, message)
287 }
288
289 pub fn unknown(code: CheckCode, message: impl Into<String>) -> Self {
290 Self::new(code, CheckStatus::Unknown, message)
291 }
292
293 pub fn info(code: CheckCode, message: impl Into<String>) -> Self {
295 Self::new(code, CheckStatus::Info, message)
296 }
297}
298
299impl CheckCode {
300 pub const fn is_revocation(self) -> bool {
307 matches!(
308 self,
309 Self::RevocationPolicy
310 | Self::RevocationNotChecked
311 | Self::RevocationOk
312 | Self::CertRevoked
313 | Self::CertRevokedAfterValidationTime
314 | Self::RevocationStatusUnknown
315 | Self::RevocationStatusUnknownByResponder
316 | Self::RevocationDataStale
317 | Self::RevocationDataInvalid
318 | Self::OcspResponderTrusted
319 | Self::OnlineFetchFailed
320 )
321 }
322}
323
324pub fn verdict_of(checks: &[Check]) -> Verdict {
330 let mut verdict = Verdict::Valid;
331 for check in checks {
332 verdict = verdict.worst(match check.status {
333 CheckStatus::Passed | CheckStatus::Info => Verdict::Valid,
334 CheckStatus::Failed => Verdict::Invalid,
335 CheckStatus::Skipped | CheckStatus::Unknown => Verdict::Indeterminate,
336 });
337 }
338 verdict
339}