Skip to main content

read_fonts/tables/
dsig.rs

1//! The [DSIG](https://learn.microsoft.com/en-us/typography/opentype/spec/dsig) table
2
3include!("../../generated/generated_dsig.rs");
4
5impl SignatureRecord {
6    /// The signature block enclosed within this record.
7    ///
8    /// The `data` argument should be retrieved from the parent table
9    /// By calling its `offset_data` method.
10    ///
11    /// Only format 1 is recognised and read successfully.
12    pub fn signature_block<'a>(&self, data: FontData<'a>) -> Option<SignatureBlockFormat1<'a>> {
13        if self.format() != 1 {
14            return None;
15        }
16        let signature = self
17            .signature_block_offset()
18            .resolve::<SignatureBlockFormat1>(data)
19            .ok()?;
20        // The inner block has to agree with the record's own length field.
21        // Where they disagree there is no saying which one to believe, so the
22        // block is not readable.
23        (self.length() == u32::try_from(signature.compute_len()).ok()?).then_some(signature)
24    }
25}
26
27impl SignatureBlockFormat1<'_> {
28    /// Return the exact byte length of this table.
29    fn compute_len(&self) -> usize {
30        // This format is contiguous, and includes no trailing data.
31        self.min_byte_range().len()
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use font_test_data::bebuffer::BeBuffer;
38
39    use super::{Dsig, PermissionFlags};
40    use crate::{FontData, FontRead};
41
42    /// An empty, dummy DSIG, as inserted by fonttools.
43    /// See <https://github.com/fonttools/fonttools/blob/ec716f11851f8d5a04e3f535b53219d97001482a/Lib/fontTools/fontBuilder.py#L823-L833>.
44    #[test]
45    fn test_empty() {
46        let buf = BeBuffer::new()
47            .push(0x1u32) // version
48            .push(0x0u16) // numSignatures
49            .push(0x0u16); // flags
50
51        let dsig = Dsig::read(FontData::new(buf.data())).unwrap();
52        assert_eq!(dsig.version(), 1);
53        assert_eq!(dsig.signature_records().len(), 0);
54        assert_eq!(dsig.flags(), PermissionFlags::empty());
55    }
56
57    // A DSIG with a single entry. For ease-of-testing, we use 0xDEADBEEF
58    // instead of a full PKCS#7 packet, as it would not be this crate's
59    // responsibility to validate it anyway.
60    #[test]
61    fn test_beef() {
62        let buf = BeBuffer::new()
63            .push(0x1_u32) // DsigHeader.version
64            .push(0x1_u16) // DsigHeader.numSignatures
65            .push(0x1_u16) // DsigHeader.flags
66            .push(0x1_u32) // SignatureRecord.format
67            .push(0xC_u32) // SignatureRecord.length
68            .push(0x14_u32) // SignatureRecord.signatureBlockOffset
69            .push(0x0_u16) // SignatureBlockFormat1.reserved1
70            .push(0x0_u16) // SignatureBlockFormat1.reserved2
71            .push(0x4_u32) // SignatureBlockFormat1.signatureLength
72            .push(0xDEADBEEF_u32); // SignatureBlockFormat1.signature
73
74        let data = FontData::new(buf.data());
75
76        let dsig = Dsig::read(data).unwrap();
77
78        assert_eq!(dsig.version(), 1);
79        assert_eq!(dsig.signature_records().len(), 1);
80        assert_eq!(dsig.flags(), PermissionFlags::CANNOT_BE_RESIGNED);
81
82        let record = dsig.signature_records()[0];
83        assert_eq!(record.format(), 1);
84
85        let block = record.signature_block(data).unwrap();
86        assert_eq!(block.signature(), &[0xDE, 0xAD, 0xBE, 0xEF]);
87    }
88
89    // A DSIG with a single entry, but in a format we do not recognise; this
90    // should fail to read.
91    #[test]
92    fn test_unknown_format() {
93        let buf = BeBuffer::new()
94            .push(0x1_u32) // DsigHeader.version
95            .push(0x1_u16) // DsigHeader.numSignatures
96            .push(0x1_u16) // DsigHeader.flags
97            .push(0x2_u32) // SignatureRecord.format, BUT AN UNRECOGNISED ONE
98            .push(0xC_u32) // SignatureRecord.length
99            .push(0x14_u32) // SignatureRecord.signatureBlockOffset
100            .push(0x0_u16) // SignatureBlockFormat1.reserved1
101            .push(0x0_u16) // SignatureBlockFormat1.reserved2
102            .push(0x4_u32) // SignatureBlockFormat1.signatureLength
103            .push(0xDEADBEEF_u32); // SignatureBlockFormat1.signature
104
105        let data = FontData::new(buf.data());
106
107        // Load the first record.
108        let dsig = Dsig::read(data).unwrap();
109
110        assert_eq!(dsig.signature_records().len(), 1);
111        let record = dsig.signature_records()[0];
112
113        // Assert that it is the unknown format '2', and that it fails to be read.
114        assert_eq!(record.format(), 2);
115
116        assert!(record.signature_block(data).is_none());
117    }
118
119    // A DSIG with a single entry, whose inner block has a different length to
120    // that which the outer record's length field prescribes; this is ambiguous,
121    // and so should fail to read.
122    #[test]
123    fn test_lying_length() {
124        let buf = BeBuffer::new()
125            .push(0x1_u32) // DsigHeader.version
126            .push(0x1_u16) // DsigHeader.numSignatures
127            .push(0x1_u16) // DsigHeader.flags
128            .push(0x1_u32) // SignatureRecord.format
129            .push(0xB_u32) // SignatureRecord.length, BUT ONE LESS THAN THE INNER LENGTH REQUIRES
130            .push(0x14_u32) // SignatureRecord.signatureBlockOffset
131            .push(0x0_u16) // SignatureBlockFormat1.reserved1
132            .push(0x0_u16) // SignatureBlockFormat1.reserved2
133            .push(0x4_u32) // SignatureBlockFormat1.signatureLength
134            .push(0xDEADBEEF_u32); // SignatureBlockFormat1.signature
135
136        let data = FontData::new(buf.data());
137
138        // Load the first record.
139        let dsig = Dsig::read(data).unwrap();
140
141        assert_eq!(dsig.signature_records().len(), 1);
142        let record = dsig.signature_records()[0];
143
144        // The inner length requires more bytes than the outer length
145        // prescribes, so the block cannot be read.
146        assert!(record.signature_block(data).is_none());
147    }
148}