parco_ws_security/
signed_info.rs

1use base64::Engine;
2use parco_xml::{Xml, xml};
3use sha1::{Digest, Sha1};
4
5use crate::{Timestamp, crypto::WSSUId};
6
7/// XML Signature metadata containing the digest of the signed content.
8#[derive(Clone, Debug)]
9pub struct SignedInfo {
10    /// Base64-encoded SHA-1 digest of the referenced XML.
11    pub digest_value: String,
12    /// the wssu id used for the wssu:Id field from the timestamp, from [`WSSUId`]
13    pub timestamp_wssu_id: WSSUId,
14}
15
16impl SignedInfo {
17    /// Computes a digest over the canonicalized timestamp XML.
18    pub fn new(timestamp: &Timestamp) -> Self {
19        let xml = timestamp.xml();
20
21        let mut hasher = Sha1::new();
22        hasher.update(xml.as_bytes());
23        let digest = hasher.finalize();
24        let digest_value = base64::engine::general_purpose::STANDARD.encode(digest);
25
26        Self {
27            digest_value,
28            timestamp_wssu_id: timestamp.wssu_id,
29        }
30    }
31}
32
33xml! {
34    use SignedInfo;
35
36    @ns {
37        dsig = "http://www.w3.org/2000/09/xmldsig#",
38    }
39
40    dsig:SignedInfo {
41        dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" {}
42        dsig:SignatureMethod  Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" {}
43        dsig:Reference URI=(self.timestamp_wssu_id.with_hash()) {
44            dsig:Transforms {
45                dsig:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" {}
46            }
47            dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" {}
48            dsig:DigestValue {
49                (self.digest_value)
50            }
51        }
52    }
53}