cdx_core/document/
security.rs1#[cfg(feature = "encryption")]
2use crate::security::EncryptionMetadata;
3
4#[cfg(feature = "signatures")]
5use crate::archive::SIGNATURES_PATH;
6#[cfg(feature = "signatures")]
7use crate::manifest::SecurityRef;
8#[cfg(feature = "signatures")]
9use crate::security::{Signature, SignatureFile};
10
11use crate::Result;
12
13use super::Document;
14use super::MutableResource;
15
16impl Document {
17 #[cfg(feature = "signatures")]
19 #[must_use]
20 pub fn signature_file(&self) -> Option<&SignatureFile> {
21 self.signature_file.as_ref()
22 }
23
24 #[cfg(feature = "signatures")]
26 #[must_use]
27 pub fn signatures(&self) -> &[Signature] {
28 self.signature_file
29 .as_ref()
30 .map_or(&[], |sf| sf.signatures.as_slice())
31 }
32
33 #[cfg(feature = "signatures")]
43 pub fn add_signature(&mut self, signature: Signature) -> Result<()> {
44 let doc_id = self.compute_id()?;
45
46 if let Some(sig_file) = self.signature_file.as_mut() {
47 sig_file.document_id = doc_id;
49 sig_file.add_signature(signature);
50 } else {
51 let mut sig_file = SignatureFile::new(doc_id);
52 sig_file.add_signature(signature);
53 self.signature_file = Some(sig_file);
54 }
55
56 self.manifest.security = Some(SecurityRef {
58 signatures: Some(SIGNATURES_PATH.to_string()),
59 encryption: self
60 .manifest
61 .security
62 .as_ref()
63 .and_then(|s| s.encryption.clone()),
64 });
65
66 Ok(())
67 }
68
69 #[cfg(feature = "signatures")]
71 #[must_use]
72 pub fn has_signatures(&self) -> bool {
73 self.signature_file
74 .as_ref()
75 .is_some_and(|sf| !sf.is_empty())
76 }
77
78 #[cfg(not(feature = "signatures"))]
82 #[must_use]
83 pub fn has_signatures(&self) -> bool {
84 false
85 }
86
87 #[cfg(feature = "encryption")]
89 #[must_use]
90 pub fn encryption_metadata(&self) -> Option<&EncryptionMetadata> {
91 self.encryption_metadata.as_ref()
92 }
93
94 #[cfg(feature = "encryption")]
96 #[must_use]
97 pub fn is_encrypted(&self) -> bool {
98 self.encryption_metadata.is_some()
99 }
100
101 #[cfg(not(feature = "encryption"))]
105 #[must_use]
106 pub fn is_encrypted(&self) -> bool {
107 false
108 }
109
110 #[cfg(feature = "encryption")]
116 pub fn set_encryption(&mut self, metadata: EncryptionMetadata) -> Result<()> {
117 self.require_mutable("set encryption")?;
118 self.encryption_metadata = Some(metadata);
119 Ok(())
120 }
121
122 #[cfg(feature = "encryption")]
128 pub fn clear_encryption(&mut self) -> Result<()> {
129 self.require_mutable("remove encryption")?;
130 self.encryption_metadata = None;
131 Ok(())
132 }
133}