summaryrefslogtreecommitdiff
path: root/remote_provisioning/hwtrust/src/cbor/rkp/device_info.rs
blob: 09b2eca3446135700446f17abeba2a0f84c3a2d5 (plain)
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
use crate::cbor::field_value::FieldValue;
use crate::rkp::{
    DeviceInfo, DeviceInfoBootloaderState, DeviceInfoSecurityLevel, DeviceInfoVbState,
};
use anyhow::{bail, ensure, Context, Result};
use ciborium::value::Value;

impl DeviceInfo {
    /// Create a new DeviceInfo struct from Values parsed by ciborium
    pub fn from_cbor_values(
        values: Vec<(Value, Value)>,
        explicit_version: Option<u32>,
    ) -> Result<Self> {
        let mut brand = FieldValue::new("brand");
        let mut manufacturer = FieldValue::new("manufacturer");
        let mut product = FieldValue::new("product");
        let mut model = FieldValue::new("model");
        let mut device = FieldValue::new("device");
        let mut vb_state = FieldValue::new("vb_state");
        let mut bootloader_state = FieldValue::new("bootloader_state");
        let mut vbmeta_digest = FieldValue::new("vbmeta_digest");
        let mut os_version = FieldValue::new("os_version");
        let mut system_patch_level = FieldValue::new("system_patch_level");
        let mut boot_patch_level = FieldValue::new("boot_patch_level");
        let mut vendor_patch_level = FieldValue::new("vendor_patch_level");
        let mut security_level = FieldValue::new("security_level");
        let mut version = FieldValue::new("version");
        let mut fused = FieldValue::new("fused");

        for (key, value) in values {
            let field_value = match key.as_text() {
                Some("brand") => &mut brand,
                Some("manufacturer") => &mut manufacturer,
                Some("product") => &mut product,
                Some("model") => &mut model,
                Some("device") => &mut device,
                Some("vb_state") => &mut vb_state,
                Some("bootloader_state") => &mut bootloader_state,
                Some("vbmeta_digest") => &mut vbmeta_digest,
                Some("os_version") => &mut os_version,
                Some("system_patch_level") => &mut system_patch_level,
                Some("boot_patch_level") => &mut boot_patch_level,
                Some("vendor_patch_level") => &mut vendor_patch_level,
                Some("security_level") => &mut security_level,
                Some("fused") => &mut fused,
                Some("version") => &mut version,
                _ => bail!("Unexpected DeviceInfo kv-pair: ({key:?},{value:?})"),
            };
            field_value.set_once(value)?;
        }

        let version = match version.into_optional_u32() {
            Ok(Some(v)) if v == explicit_version.unwrap_or(v) => v,
            Ok(Some(v)) => bail!(
                "Parsed DeviceInfo version '{v}' does not match expected version \
                '{explicit_version:?}'"
            ),
            Ok(None) => explicit_version.context("missing required version")?,
            Err(e) => return Err(e.into()),
        };

        let security_level = match security_level.into_optional_string()? {
            Some(s) => Some(s.as_str().try_into()?),
            None => None,
        };

        let info = DeviceInfo {
            brand: brand.into_string()?,
            manufacturer: manufacturer.into_string()?,
            product: product.into_string()?,
            model: model.into_string()?,
            device: device.into_string()?,
            vb_state: vb_state.into_string()?.as_str().try_into()?,
            bootloader_state: bootloader_state.into_string()?.as_str().try_into()?,
            vbmeta_digest: vbmeta_digest.into_bytes()?,
            os_version: os_version.into_optional_string()?,
            system_patch_level: system_patch_level.into_u32()?,
            boot_patch_level: boot_patch_level.into_u32()?,
            vendor_patch_level: vendor_patch_level.into_u32()?,
            security_level,
            fused: fused.into_bool()?,
            version: version.try_into()?,
        };
        info.validate_avf_fields()?;
        Ok(info)
    }

    fn validate_avf_fields(&self) -> Result<()> {
        if Some(DeviceInfoSecurityLevel::Avf) == self.security_level {
            ensure!(
                self.bootloader_state == DeviceInfoBootloaderState::Avf
                    && self.vb_state == DeviceInfoVbState::Avf
                    && self.brand == "aosp-avf"
                    && self.device == "avf"
                    && self.model == "avf"
                    && self.manufacturer == "aosp-avf"
                    && self.product == "avf",
                "AVF security level requires AVF fields. Got: {:?}",
                self
            );
        } else {
            ensure!(
                self.bootloader_state != DeviceInfoBootloaderState::Avf
                    && self.vb_state != DeviceInfoVbState::Avf
                    && self.brand != "aosp-avf"
                    && self.device != "avf"
                    && self.model != "avf"
                    && self.manufacturer != "aosp-avf"
                    && self.product != "avf",
                "Non-AVF security level requires non-AVF fields. Got: {:?}",
                self
            );
        }
        Ok(())
    }
}

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

    #[test]
    fn device_info_from_cbor_values_optional_os_version() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "os_version");
        let info = DeviceInfo::from_cbor_values(values, None).unwrap();
        assert!(info.os_version.is_none());
    }

    #[test]
    fn device_info_from_cbor_values_missing_required_field() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "brand");
        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("brand"));
    }

    #[test]
    fn from_cbor_values_valid_v2() {
        let actual = DeviceInfo::from_cbor_values(get_valid_values(), None).unwrap();
        let expected = DeviceInfo {
            brand: "generic".to_string(),
            manufacturer: "acme".to_string(),
            product: "phone".to_string(),
            model: "the best one".to_string(),
            device: "really the best".to_string(),
            vb_state: DeviceInfoVbState::Green,
            bootloader_state: DeviceInfoBootloaderState::Locked,
            vbmeta_digest: b"abcdefg".to_vec(),
            os_version: Some("dessert".to_string()),
            system_patch_level: 303010,
            boot_patch_level: 30300102,
            vendor_patch_level: 30300304,
            security_level: Some(DeviceInfoSecurityLevel::Tee),
            fused: true,
            version: DeviceInfoVersion::V2,
        };
        assert_eq!(actual, expected);
    }

    #[test]
    fn device_info_from_cbor_values_valid_v3() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "version");
        let actual = DeviceInfo::from_cbor_values(values, Some(3)).unwrap();
        let expected = DeviceInfo {
            brand: "generic".to_string(),
            manufacturer: "acme".to_string(),
            product: "phone".to_string(),
            model: "the best one".to_string(),
            device: "really the best".to_string(),
            vb_state: DeviceInfoVbState::Green,
            bootloader_state: DeviceInfoBootloaderState::Locked,
            vbmeta_digest: b"abcdefg".to_vec(),
            os_version: Some("dessert".to_string()),
            system_patch_level: 303010,
            boot_patch_level: 30300102,
            vendor_patch_level: 30300304,
            security_level: Some(DeviceInfoSecurityLevel::Tee),
            fused: true,
            version: DeviceInfoVersion::V3,
        };
        assert_eq!(actual, expected);
    }

    #[test]
    fn device_info_from_cbor_values_mismatched_version() {
        let values: Vec<(Value, Value)> = get_valid_values();
        let err = DeviceInfo::from_cbor_values(values, Some(3)).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("version"));
    }

    #[test]
    fn device_info_from_cbor_values_invalid_version_value() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "version");
        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("version"));
    }

    #[test]
    fn device_info_from_cbor_values_invalid_explicit_version() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "version");
        let err = DeviceInfo::from_cbor_values(values, Some(0)).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("version"));
    }

    #[test]
    fn device_info_from_cbor_values_missing_version() {
        let values: Vec<(Value, Value)> = get_valid_values_filtered(|x| x != "version");
        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("version"));
    }

    #[test]
    fn device_info_from_cbor_duplicate_values() {
        let mut values: Vec<(Value, Value)> = get_valid_values();
        values.push(("brand".into(), "generic".into()));

        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        println!("{err:?}");
        assert!(err.to_string().contains("may be set only once"));
    }

    #[test]
    fn device_info_from_cbor_values_non_avf_security_level_has_avf_vb_state() {
        let mut values = get_valid_values_filtered(|x| x != "vb_state");
        values.push(("vb_state".into(), "avf".into()));

        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        assert!(err.to_string().contains("Non-AVF security level"), "{err:?}");
    }

    #[test]
    fn device_info_from_cbor_values_non_avf_security_level_has_avf_bootloader_state() {
        let mut values = get_valid_values_filtered(|x| x != "bootloader_state");
        values.push(("bootloader_state".into(), "avf".into()));

        let err = DeviceInfo::from_cbor_values(values, None).unwrap_err();
        assert!(err.to_string().contains("Non-AVF security level"), "{err:?}");
    }

    #[test]
    fn device_info_from_cbor_values_avf_security_level_has_non_avf_vb_state() {
        let values: Vec<(Value, Value)> = get_valid_avf_values()
            .into_iter()
            .filter(|(k, _v)| k.as_text().unwrap() != "vb_state")
            .chain(vec![("vb_state".into(), "green".into())])
            .collect();
        let err = DeviceInfo::from_cbor_values(values, Some(3)).unwrap_err();
        assert!(err.to_string().contains("AVF security level requires AVF fields"), "{err:?}");
    }

    #[test]
    fn device_info_from_cbor_values_avf_security_level_has_avf_fields() {
        let values = get_valid_avf_values();
        let actual = DeviceInfo::from_cbor_values(values, Some(3)).unwrap();
        let expected = DeviceInfo {
            brand: "aosp-avf".to_string(),
            manufacturer: "aosp-avf".to_string(),
            product: "avf".to_string(),
            model: "avf".to_string(),
            device: "avf".to_string(),
            vb_state: DeviceInfoVbState::Avf,
            bootloader_state: DeviceInfoBootloaderState::Avf,
            vbmeta_digest: b"abcdefg".to_vec(),
            os_version: Some("dessert".to_string()),
            system_patch_level: 303010,
            boot_patch_level: 30300102,
            vendor_patch_level: 30300304,
            security_level: Some(DeviceInfoSecurityLevel::Avf),
            fused: true,
            version: DeviceInfoVersion::V3,
        };
        assert_eq!(expected, actual);
    }

    fn get_valid_values() -> Vec<(Value, Value)> {
        vec![
            ("brand".into(), "generic".into()),
            ("manufacturer".into(), "acme".into()),
            ("product".into(), "phone".into()),
            ("model".into(), "the best one".into()),
            ("device".into(), "really the best".into()),
            ("vb_state".into(), "green".into()),
            ("bootloader_state".into(), "locked".into()),
            ("vbmeta_digest".into(), b"abcdefg".as_ref().into()),
            ("os_version".into(), "dessert".into()),
            ("system_patch_level".into(), 303010.into()),
            ("boot_patch_level".into(), 30300102.into()),
            ("vendor_patch_level".into(), 30300304.into()),
            ("security_level".into(), "tee".into()),
            ("fused".into(), 1.into()),
            ("version".into(), 2.into()),
        ]
    }

    fn get_valid_avf_values() -> Vec<(Value, Value)> {
        vec![
            ("brand".into(), "aosp-avf".into()),
            ("manufacturer".into(), "aosp-avf".into()),
            ("product".into(), "avf".into()),
            ("model".into(), "avf".into()),
            ("device".into(), "avf".into()),
            ("vb_state".into(), "avf".into()),
            ("bootloader_state".into(), "avf".into()),
            ("vbmeta_digest".into(), b"abcdefg".as_ref().into()),
            ("os_version".into(), "dessert".into()),
            ("system_patch_level".into(), 303010.into()),
            ("boot_patch_level".into(), 30300102.into()),
            ("vendor_patch_level".into(), 30300304.into()),
            ("security_level".into(), "avf".into()),
            ("fused".into(), 1.into()),
        ]
    }

    fn get_valid_values_filtered<F: Fn(&str) -> bool>(filter: F) -> Vec<(Value, Value)> {
        get_valid_values().into_iter().filter(|x| filter(x.0.as_text().unwrap())).collect()
    }
}