suit_validator 0.1.3

no_std-friendly Rust crate for decoding and verifying SUIT Manifest.
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! struct to decode "flat" op/arg sequences used by SUIT:
//! - SUIT_Shared_Sequence (flat: [ op, arg, op, arg, ... ])
//! - SUIT_Command_Sequence (flat: [ op, arg, op, arg, ... ])
use crate::{
    errors::SuitError,
    suit_manifest::{
        BstrSuitCommandSequence, BstrSuitSharedSequence, CommandCustomValue, IndexArg,
        SuitCondition, SuitDirective, SuitDirectiveTryEachArgument,
        SuitDirectiveTryEachArgumentShared, SuitParameters, SuitRepPolicy, SuitSharedCommand,
    },
};
use heapless::Vec;
use minicbor::{Decode, Decoder, data::Type, decode::Error as DecodeError};

/// Wrapping structure around the inner CBOR flat array bytes.
#[derive(Debug)]
pub(crate) struct FlatSequence<'a>(pub(crate) &'a [u8]);

impl<'b, C> Decode<'b, C> for FlatSequence<'b> {
    fn decode(d: &mut Decoder<'b>, _ctx: &mut C) -> Result<Self, DecodeError> {
        let start = d.position();
        let input = d.input();
        // We advance the main decoder to the next element
        d.skip()?;

        let end = d.position();

        let inner = &input[start..end];
        Ok(FlatSequence(inner))
    }
}

impl<'b> FlatSequence<'b> {
    /// Collect the pairs relative to the [`FlatSequence`] inner bytes.
    pub(crate) fn collect_pairs<const N: usize>(&self) -> Result<Vec<Pair<'b>, N>, SuitError> {
        let mut d = Decoder::new(self.0);
        d.array()
            .map_err(|e| e.with_message("expected top level array in Sequence"))?;
        let mut out: Vec<Pair<'b>, N> = Vec::new();
        loop {
            match read_next_pair(&mut d) {
                Ok(Some(pair)) => {
                    out.push(pair).map_err(|_| SuitError::out_of_space(N))?;
                }
                Ok(None) => break,
                Err(e) => return Err(e),
            }
        }
        Ok(out)
    }
}

/// Read the next flat op/arg sequence and stock it into a `Pair`
fn read_next_pair<'b>(d: &mut Decoder<'b>) -> Result<Option<Pair<'b>>, SuitError> {
    match read_op_id(d)? {
        None => Ok(None),
        Some(op) => {
            let start = d.position();
            // we advance to next element
            d.skip()?;
            let end = d.position();
            // we get the bytes input from the next element
            let bytes = &d.input()[start..end];
            Ok(Some(Pair { op, bytes }))
        }
    }
}

/// helper to read an op id (accept unsigned or negative int)
fn read_op_id<'b>(d: &mut Decoder<'b>) -> Result<Option<i64>, SuitError> {
    match d.datatype() {
        Ok(ty) => match ty {
            Type::U8 | Type::U16 | Type::U32 | Type::U64 => {
                let u = d.u64()?;
                let i = i64::try_from(u).map_err(|_| DecodeError::message("op id too large"))?;
                Ok(Some(i))
            }
            Type::I8 | Type::I16 | Type::I32 | Type::I64 => {
                let i = d.i64()?;
                Ok(Some(i))
            }
            _ => Err(DecodeError::type_mismatch(ty)
                .with_message("expected integer op  id")
                .into()),
        },
        Err(e) => {
            if e.is_end_of_input() {
                Ok(None)
            } else {
                Err(e.into())
            }
        }
    }
}

#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Pair<'b> {
    op: i64,
    bytes: &'b [u8],
}

// We want to be able to decode into a SuitCondition when iterrating using `next()``
impl<'b> TryFrom<&Pair<'b>> for SuitCondition {
    type Error = SuitError;
    fn try_from(pair: &Pair<'b>) -> Result<SuitCondition, Self::Error> {
        let _ctx = &mut ();
        let mut dec = Decoder::new(pair.bytes);
        match pair.op {
            1 => Ok(SuitCondition::VendorIdentifier(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            2 => Ok(SuitCondition::ClassIdentifier(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            3 => Ok(SuitCondition::ImageMatch(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            5 => Ok(SuitCondition::ComponentSlot(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            6 => Ok(SuitCondition::CheckContent(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            14 => Ok(SuitCondition::Abort(SuitRepPolicy::decode(&mut dec, _ctx)?)),
            24 => Ok(SuitCondition::DeviceIdentifier(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            _ => Err(SuitError::unknown_op(pair.op).with_ctx("SuitCondition")),
        }
    }
}

impl<'b> TryFrom<&Pair<'b>> for SuitSharedCommand<'b> {
    type Error = SuitError;
    fn try_from(pair: &Pair<'b>) -> Result<Self, Self::Error> {
        let _ctx = &mut ();
        let mut dec = Decoder::new(pair.bytes);
        match pair.op {
            12 => Ok(SuitSharedCommand::SetComponentIndex(IndexArg::decode(
                &mut dec, _ctx,
            )?)),
            32 => Ok(SuitSharedCommand::RunSequence(
                BstrSuitSharedSequence::decode(&mut dec, _ctx)?,
            )),
            15 => Ok(SuitSharedCommand::TryEach(
                SuitDirectiveTryEachArgumentShared::decode(&mut dec, _ctx)?,
            )),
            20 => Ok(SuitSharedCommand::OverrideParameters(
                SuitParameters::decode(&mut dec, _ctx)?,
            )),
            _ => Err(SuitError::unknown_op(pair.op).with_ctx("SuitSharedCommand")),
        }
    }
}

impl<'b> TryFrom<&Pair<'b>> for SuitDirective<'b> {
    type Error = SuitError;
    fn try_from(pair: &Pair<'b>) -> Result<Self, Self::Error> {
        let _ctx = &mut ();
        let mut dec = Decoder::new(pair.bytes);
        match pair.op {
            18 => Ok(SuitDirective::Write(SuitRepPolicy::decode(&mut dec, _ctx)?)),
            12 => Ok(SuitDirective::SetComponentIndex(IndexArg::decode(
                &mut dec, _ctx,
            )?)),
            32 => Ok(SuitDirective::RunSequence(BstrSuitCommandSequence::decode(
                &mut dec, _ctx,
            )?)),
            15 => Ok(SuitDirective::TryEach(
                SuitDirectiveTryEachArgument::decode(&mut dec, _ctx)?,
            )),
            20 => Ok(SuitDirective::OverrideParameters(SuitParameters::decode(
                &mut dec, _ctx,
            )?)),
            21 => Ok(SuitDirective::Fetch(SuitRepPolicy::decode(&mut dec, _ctx)?)),
            22 => Ok(SuitDirective::Copy(SuitRepPolicy::decode(&mut dec, _ctx)?)),
            31 => Ok(SuitDirective::Swap(SuitRepPolicy::decode(&mut dec, _ctx)?)),
            23 => Ok(SuitDirective::Invoke(SuitRepPolicy::decode(
                &mut dec, _ctx,
            )?)),
            _ => Err(SuitError::unknown_op(pair.op).with_ctx("SuitDirective")),
        }
    }
}

impl<'b> TryFrom<&Pair<'b>> for CommandCustomValue<'b> {
    type Error = SuitError;
    fn try_from(pair: &Pair<'b>) -> Result<CommandCustomValue<'b>, Self::Error> {
        let mut d = Decoder::new(pair.bytes);
        Ok(CommandCustomValue::decode(&mut d, &mut ())?)
    }
}

/// A small view returned by *targeted* iterators.
/// Carries the `op` so caller can inspect it before decoding, using [`PairView::op()`].
///
/// It exposes [`PairView::get()`] which returns the concrete target type without
/// requiring a turbofish because the iterator's Item is already specialized with `T`.
#[derive(Debug, Clone, Copy)]
pub struct PairView<'b, T> {
    pair: &'b Pair<'b>,
    _ty: core::marker::PhantomData<&'b T>,
}
impl<'b, T> PairView<'b, T>
where
    T: TryFrom<&'b Pair<'b>, Error = SuitError>,
{
    fn new(pair: &'b Pair<'b>) -> Self {
        Self {
            pair,
            _ty: core::marker::PhantomData,
        }
    }

    ///  *Op* of the current [`PairView`].
    pub fn op(&self) -> i64 {
        self.pair.op
    }

    /// *Inner bytes* of the [`PairView`]
    pub fn bytes(&self) -> &'b [u8] {
        self.pair.bytes
    }
    /// Decodes and returns the concrete value of type `T`.
    ///
    /// Returns an error if the pair cannot be converted into `T`.
    pub fn get(&self) -> Result<T, SuitError> {
        T::try_from(self.pair)
    }
}

pub(crate) fn iter_conditions<'b>(
    pairs: &'b [Pair<'b>],
) -> impl Iterator<Item = PairView<'b, SuitCondition>> {
    pairs
        .iter()
        .filter(|p| matches!(p.op, 1 | 2 | 3 | 5 | 6 | 14 | 24))
        .map(PairView::new)
}

pub(crate) fn iter_directives<'b>(
    pairs: &'b [Pair<'b>],
) -> impl Iterator<Item = PairView<'b, SuitDirective<'b>>> {
    pairs
        .iter()
        .filter(|p| matches!(p.op, 12 | 15 | 18 | 20 | 21 | 22 | 23 | 31 | 32))
        .map(PairView::new)
}

pub(crate) fn iter_shared_commands<'b>(
    pairs: &'b [Pair<'b>],
) -> impl Iterator<Item = PairView<'b, SuitSharedCommand<'b>>> {
    pairs
        .iter()
        .filter(|p| matches!(p.op, 12 | 32 | 15 | 20))
        .map(PairView::new)
}

pub(crate) fn iter_custom<'b>(
    pairs: &'b [Pair<'b>],
) -> impl Iterator<Item = PairView<'b, CommandCustomValue<'b>>> {
    pairs.iter().filter(|p| p.op < 0).map(PairView::new)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[allow(dead_code)]
    const FLAT_SEQUENCE: cboritem::CborItem<'static> = cbor_macro::cbo!(
        r#"[
                / directive-override-parameters / 20,{
                    / uri / 21:"http://example.com/file.bin"
                },
                / directive-fetch / 21,2,
                / condition-image-match / 3,15
            ]"#
    );

    #[allow(dead_code)]
    fn get_test_flat_seq_decoder(cbor_bytes: &'static [u8]) -> Decoder<'static> {
        let mut d = Decoder::new(cbor_bytes);
        d.array().expect("expect top level array");
        d
    }

    #[test]
    fn decode_advances_main_decoder() {
        let ci = cbor_macro::cbo!(r#"[ [1,2] ]"#);
        let mut main = Decoder::new(&ci);
        let _fsd = main
            .decode::<FlatSequence>()
            .expect("Expected top level array");
        assert!(
            main.datatype()
                .expect_err("Expected an Error")
                .is_end_of_input()
        );
    }

    #[test]
    fn test_read_op() {
        let mut decoder_on_op =
            get_test_flat_seq_decoder(&cbor_macro::cbo!(r#"[1, 2, ["not and int"]]"#));
        // we start decoding the array as FlatSequence would do
        assert_eq!(
            1,
            read_op_id(&mut decoder_on_op)
                .unwrap()
                .expect("Missing op id")
        );
        assert_eq!(
            2,
            read_op_id(&mut decoder_on_op)
                .unwrap()
                .expect("Missing op id")
        );
        assert!(read_op_id(&mut decoder_on_op).is_err_and(|e| e.is_decode_error()))
    }

    #[test]
    fn test_read_next_two() {
        let mut d = get_test_flat_seq_decoder(&FLAT_SEQUENCE);
        let firt_pair = read_next_pair(&mut d).unwrap().expect("Missing pair");
        let first_expected_pair = Pair {
            op: 20,
            bytes: &cbor_macro::cbo!(
                r#"{
                    / uri / 21:"http://example.com/file.bin"
                }"#
            ),
        };

        let second_pair = read_next_pair(&mut d).unwrap().expect("Missing pair");
        let second_expected_pair = Pair {
            op: 21,
            bytes: &[2u8],
        };

        let third_pair = read_next_pair(&mut d).unwrap().expect("Missing Pair");
        let third_expected_pair = Pair {
            op: 3,
            bytes: &[15u8],
        };
        assert_eq!(first_expected_pair, firt_pair);
        assert_eq!(second_expected_pair, second_pair);
        assert_eq!(third_expected_pair, third_pair);

        // No more item
        assert!(read_next_pair(&mut d).unwrap().is_none());
    }

    #[test]
    fn test_collect_pairs() {
        let test_flat_seq = FlatSequence(&cbor_macro::cbo!(r#"[1,2,3,4]"#));
        let collected = test_flat_seq.collect_pairs::<2>().unwrap();

        assert_eq!(
            Pair {
                op: 1,
                bytes: &[2u8]
            },
            collected[0]
        );
        assert_eq!(
            Pair {
                op: 3,
                bytes: &[4u8]
            },
            collected[1]
        );

        let test_flat_seq = FlatSequence(&cbor_macro::cbo!(r#"[1,2,3,4]"#));
        assert!(
            test_flat_seq
                .collect_pairs::<0>()
                .is_err_and(|e| e.is_out_of_space())
        );
    }

    #[test]
    fn test_iterator_conditions_correctly() {
        let ci = cbor_macro::cbo!(r#"[1, 15, 2, 15] "#);
        let pairs = FlatSequence(&ci).collect_pairs::<4>().unwrap();
        let mut cond_iter = iter_conditions(&pairs);
        let first_cond = cond_iter.next().expect("Expected first condition");
        let second_cond = cond_iter.next().expect("Expected second condition");

        assert_eq!(first_cond.op(), 1);
        assert_eq!(second_cond.op(), 2);
        assert!(matches!(
            first_cond
                .get()
                .expect("First condition decoding should be ok"),
            SuitCondition::VendorIdentifier(_)
        ));
        assert!(matches!(
            second_cond
                .get()
                .expect("Second condition decoding should be ok"),
            SuitCondition::ClassIdentifier(_)
        ));

        assert!(cond_iter.next().is_none());
    }

    #[test]
    fn from_pair_unknown_op_errors() {
        let p = Pair {
            op: -10,
            bytes: &[0u8],
        };

        assert!(<SuitCondition>::try_from(&p).is_err_and(|e| e.is_unknown_op()));
        assert!(<SuitSharedCommand>::try_from(&p).is_err_and(|e| e.is_unknown_op()));
        assert!(<SuitDirective>::try_from(&p).is_err_and(|e| e.is_unknown_op()));
        assert!(
            <CommandCustomValue>::try_from(&p)
                .is_ok_and(|res| matches!(res, CommandCustomValue::Integer(_)))
        );
    }
}