1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! signature index construction as builder pattern

use super::*;

use super::IndexEntry;
use crate::constants::*;
use std::default::Default;

/// A marker trait for builder stages
pub trait ConstructionStage {}

/// Initial empty builder.
pub struct Empty;
/// Builder beyond the empty stage, already containing a digest.
///
/// Implies that headers and content are complete.
pub struct WithDigest;

/// Builder already has a hash and is ready for completion.
pub struct WithSignature;

impl ConstructionStage for Empty {}

impl ConstructionStage for WithDigest {}

impl ConstructionStage for WithSignature {}

/// base signature header builder
///
/// T describes the stage and can be one of `Empty`, `WithDigest`, `WithSignature`
pub struct SignatureHeaderBuilder<T>
where
    T: ConstructionStage,
{
    entries: Vec<IndexEntry<IndexSignatureTag>>,
    phantom: std::marker::PhantomData<T>,
}

impl SignatureHeaderBuilder<Empty> {
    pub fn new() -> Self {
        Self {
            entries: Vec::with_capacity(10),
            phantom: Default::default(),
        }
    }
}

impl Default for SignatureHeaderBuilder<Empty> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> SignatureHeaderBuilder<T>
where
    T: ConstructionStage,
{
    /// Construct the complete signature header.
    pub fn build(mut self, headers_plus_payload_size: usize) -> Header<IndexSignatureTag> {
        let entry = match headers_plus_payload_size.try_into() {
            Ok(size) => IndexEntry::new(
                IndexSignatureTag::RPMSIGTAG_SIZE,
                0i32,
                IndexData::Int32(vec![size]),
            ),
            Err(_) => IndexEntry::new(
                IndexSignatureTag::RPMSIGTAG_LONGSIZE,
                0i32,
                IndexData::Int64(vec![headers_plus_payload_size as u64]),
            ),
        };
        self.entries.insert(0, entry);

        Header::<IndexSignatureTag>::from_entries(
            self.entries,
            IndexSignatureTag::HEADER_SIGNATURES,
        )
    }
}

impl SignatureHeaderBuilder<Empty> {
    /// add a digest over the header and a signature across header and source excluding the static lead
    pub fn add_digest(
        mut self,
        digest_header_sha1: &str,
        digest_header_sha256: &str,
        digest_header_and_archive: &[u8],
    ) -> SignatureHeaderBuilder<WithDigest> {
        let offset = 0i32; // filled externally later on
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_MD5,
            offset,
            IndexData::Bin(digest_header_and_archive.to_vec()),
        ));
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_SHA1,
            offset,
            IndexData::StringTag(digest_header_sha1.to_string()),
        ));
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_SHA256,
            offset,
            IndexData::StringTag(digest_header_sha256.to_string()),
        ));
        SignatureHeaderBuilder::<WithDigest> {
            entries: self.entries,
            phantom: Default::default(),
        }
    }
}

impl SignatureHeaderBuilder<WithDigest> {
    /// add a signature over the header and a signature across header and source excluding the static lead
    pub fn add_rsa_signature(
        mut self,
        sig_header_only: &[u8],
        sig_header_and_archive: &[u8],
    ) -> SignatureHeaderBuilder<WithSignature> {
        let offset = 0i32; // filled externally later on
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_RSA,
            offset,
            IndexData::Bin(sig_header_only.to_vec()),
        ));
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_PGP,
            offset,
            IndexData::Bin(sig_header_and_archive.to_vec()),
        ));
        SignatureHeaderBuilder::<WithSignature> {
            entries: self.entries,
            phantom: Default::default(),
        }
    }

    pub fn add_eddsa_signature(
        mut self,
        sig_header_only: &[u8],
    ) -> SignatureHeaderBuilder<WithSignature> {
        let offset = 0i32; // filled externally later on
        self.entries.push(IndexEntry::new(
            IndexSignatureTag::RPMSIGTAG_DSA,
            offset,
            IndexData::Bin(sig_header_only.to_vec()),
        ));
        SignatureHeaderBuilder::<WithSignature> {
            entries: self.entries,
            phantom: Default::default(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn signature_builder_w_digest_and_rsa_signature() {
        let builder = SignatureHeaderBuilder::<Empty>::new();

        let sig_header_only = [0u8; 32];
        let sig_header_and_archive = [0u8; 32];

        let digest_header_sha1 = hex::encode([0u8; 64]);
        let digest_header_sha256: String = hex::encode([0u8; 64]);

        let digest_header_and_archive = [0u8; 64];

        let header = builder
            .add_digest(
                digest_header_sha1.as_str(),
                digest_header_sha256.as_str(),
                &digest_header_and_archive[..],
            )
            .add_rsa_signature(&sig_header_only[..], &sig_header_and_archive[..])
            .build(32);

        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_RSA)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_PGP)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_MD5)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA1)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA256)
            .is_ok());
    }

    #[test]
    fn signature_builder_w_digest_and_eddsa_signature() {
        let builder = SignatureHeaderBuilder::<Empty>::new();

        let sig_header_only = [0u8; 32];

        let digest_header_sha1 = hex::encode([0u8; 64]);
        let digest_header_sha256: String = hex::encode([0u8; 64]);

        let digest_header_and_archive = [0u8; 64];

        let header = builder
            .add_digest(
                digest_header_sha1.as_str(),
                digest_header_sha256.as_str(),
                &digest_header_and_archive[..],
            )
            .add_eddsa_signature(&sig_header_only[..])
            .build(32);

        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_DSA)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_MD5)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA1)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA256)
            .is_ok());
    }

    #[test]
    fn signature_builder_digest_only() {
        let builder = SignatureHeaderBuilder::<Empty>::new();

        let digest_header_sha1 = hex::encode([0u8; 64]);
        let digest_header_sha256: String = hex::encode([0u8; 64]);

        let digest_header_and_archive = [0u8; 64];

        let header = builder
            .add_digest(
                digest_header_sha1.as_str(),
                digest_header_sha256.as_str(),
                &digest_header_and_archive[..],
            )
            .build(32);

        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_RSA)
            .is_err());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_PGP)
            .is_err());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_MD5)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA1)
            .is_ok());
        assert!(header
            .find_entry_or_err(IndexSignatureTag::RPMSIGTAG_SHA256)
            .is_ok());
    }

    // @todo: this test is kind of duplicative, probably not necessary?
    #[cfg(feature = "signature-meta")]
    #[test]
    fn signature_header_build() {
        let size: u32 = 209_348;
        let digest_header_and_archive: &[u8] = &[22u8; 16];
        let digest_header_sha1 = "5A884F0CB41EC3DA6D6E7FC2F6AB9DECA8826E8D";
        let digest_header_sha256 =
            "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
        let sig_header_only: &[u8] = b"111222333444";
        let sig_header_and_archive: &[u8] = b"7777888899990000";

        let truth = {
            let offset = 0;
            let entries = vec![
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_SIZE,
                    offset,
                    IndexData::Int32(vec![size]),
                ),
                // TODO consider dropping md5 in favour of sha256
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_MD5,
                    offset,
                    IndexData::Bin(digest_header_and_archive.to_vec()),
                ),
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_SHA1,
                    offset,
                    IndexData::StringTag(digest_header_sha1.to_owned()),
                ),
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_SHA256,
                    offset,
                    IndexData::StringTag(digest_header_sha256.to_owned()),
                ),
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_RSA,
                    offset,
                    IndexData::Bin(sig_header_only.to_vec()),
                ),
                IndexEntry::new(
                    IndexSignatureTag::RPMSIGTAG_PGP,
                    offset,
                    IndexData::Bin(sig_header_and_archive.to_vec()),
                ),
            ];
            Header::<IndexSignatureTag>::from_entries(entries, IndexSignatureTag::HEADER_SIGNATURES)
        };

        let built = Header::<IndexSignatureTag>::builder()
            .add_digest(
                digest_header_sha1,
                digest_header_sha256,
                digest_header_and_archive,
            )
            .add_rsa_signature(sig_header_only, sig_header_and_archive)
            .build(size as usize);

        assert_eq!(built, truth);
    }
}